<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>书籍管理</title>
    <link rel="stylesheet" type="text/css" href="../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../css/main.css"/>
    <link rel="stylesheet" type="text/css" href="../css/bookManage.css"/>
    <script type="text/javascript" src="../js/modernizr.min.js"></script>
    <script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
    <script>
        $(function () {
            //调用公共header
            $('#left').load('left.html');
            $('#header').load('header.html');
        });
    </script>
    <script>
        //删除
        function del(uid) {
            // alert(bid);
            if (confirm("确定删除该条记录吗？")) {
                $.get("../user/delUser.do", {uid: uid}, function (data) {
                    if (data.errorType == 0) {
                        alert(data.errorInfo);
                        location.reload();
                    } else {
                        alert(data.errorInfo);
                    }
                });
            }
        }

        //修改状态
        function upStatus(status,uid){
            $.post("../user/upStatus.do",{status:status,uid:uid},function (data) {
                if(data != null){
                    alert(data.errorInfo);
                    location.reload();
                }
            });
        }

        function loadUserManage(start, utype, name) {
            $.get("../user/getUsers.do", {start: start, uType: utype, name: name}, function (data) {
                var trs = "<tr>\n" +
                    "          <th>波克Id&nbsp;&nbsp;</th>\n" +
                    "          <th>姓名&nbsp;&nbsp;</th>\n" +
                    "          <th>用户名&nbsp;&nbsp;</th>\n" +
                    "          <th>性别&nbsp;&nbsp;</th>\n" +
                    "          <th>出生日期&nbsp;</th>\n" +
                    "          <th>电话号码</th>\n" +
                    "          <th>邮箱</th>\n" +
                    "          <th>用户类别</th>\n" +
                    "          <th>用户状态</th>\n" +
                    "          <th>操作&nbsp;&nbsp;</th>\n" +
                    "      </tr>";
                if (data == null) {
                    alert("操作失败，您的权限不够。。。");
                } else { //权限合格  返回不为空有数据
                    for (var i = 0; i < data.list.length; i++) { //所有查出来的书籍集合
                        //处理特殊数据
                        var sex_text = "";
                        if (data.list[i].sex == 0) {
                            sex_text = "男";
                        } else if (data.list[i].sex == 1) {
                            sex_text = "女";
                        }
                        var uType_text = "";
                        if (data.list[i].uType == 0) {
                            uType_text = "会员";
                        } else if (data.list[i].uType == 1) {
                            uType_text = "商家";
                        } else if (data.list[i].uType == 2) {
                            uType_text = "系统管理员";
                        }
                        var status_text = "";
                        //定义用户禁用功能模块
                        var a = "";
                        if (data.list[i].status == 0) {
                            status_text = "未激活";
                        } else if (data.list[i].status == 1) {
                            status_text = "可用";
                            a = "<a href='javascript:;' onclick='upStatus(2,"+data.list[i].status + "," + data.list[i].uid +")' >禁用&nbsp;&nbsp;</a>";
                        } else if (data.list[i].status == 2) {
                            status_text = "禁用";
                            a = "<a href='javascript:;' onclick='upStatus(1," + data.list[i].uid +")'>恢复使用&nbsp;&nbsp;</a>";
                        }

                        trs += "<tr>" +
                            "       <td>" + data.list[i].uid + "</td>\n" +
                            "       <td>" + data.list[i].name + "</td>\n" +
                            "       <td>" + data.list[i].username + "</td>\n" +
                            "       <td>" + sex_text + "</td>\n" +
                            "       <td>" + data.list[i].birthday + "</td>\n" +
                            "       <td>" + data.list[i].phone + "</td>\n" +
                            "       <td>" + data.list[i].email + "</td>\n" +
                            "       <td>" + uType_text + "</td>\n" +
                            "       <td>" + status_text + "</td>\n" +
                            "       <td>" +
                            "           <a href='updateUser.html?uid=" + data.list[i].uid + "'>修改&nbsp;&nbsp;</a>" + a +
                            "           <a href='javascript:;' onclick='del(" + data.list[i].uid + ")'>删除&nbsp;&nbsp;</a>" +
                            "       </td>\n" +
                            "</tr>";
                    }
                    //表格页
                    $("#users").html(trs);
                    //分页
                    var lis = "<li>\n" +
                        "                <a href=\"#\" aria-label=\"Previous\" onclick='loadUserManage(" + data.prePage + ")'>\n" +
                        "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                        "                </a>\n" +
                        "            </li>";
                    if (data.prePage == 0) {
                        lis = "<li class='disabled'>\n" +
                            "                <a href=\"#\" aria-label=\"Previous\">\n" +
                            "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    }
                    for (var i = 1; i <= data.lastPage; i++) {
                        lis += "<li><a href=\"#\"  onclick='loadUserManage(" + i + ")'>" + i + "</a></li>";
                    }
                    //结尾
                    if (data.nextPage == 0) { //下一页大于最后一页时  按钮禁用
                        lis += "<li class='disabled'>\n" +
                            "                <a href=\"#\" aria-label=\"Next\" >\n" +
                            "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    } else {
                        lis += "<li>\n" +
                            "                <a href='#' onclick='loadUserManage(" + data.nextPage + ")' aria-label=\"Next\">\n" +
                            "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    }
                    //生成
                    $("#pagination").html(lis);
                    $("#tips").html("当前第" + data.pageNum + "页，共" + data.pages + "页，" + data.total + "条数据");
                }
            });
        }

        //入口函数
        $(function () {
            //填充类型
            //加载主体
            loadUserManage(1);
            //搜索
            $("#sub").click(function () {
                var uType = $("#uType").val();//获取搜索框的值
                // alert(bname);
                var name = $("#name").val();
                // alert(btype);
                loadUserManage(1, uType, name);
            });

        });
    </script>
</head>
<body>
<div class="container clearfix">
    <div id="header"></div>
    <div id="left"></div>
    <!--/sidebar-->
    <div class="main-wrap">
        <div class="crumb-wrap">
            <div class="crumb-list"><i class="icon-font">&#xe014;</i><a href="javascript:;">用户管理</a>
            </div>
        </div>
        <div class="search-wrap">
            <!--nav 导航-->
            <div class="search-wrap">
                <div class="search-content">
                    <table class="search-tab">
                        <tr>
                            <th width="120">用户类别：</th>
                            <td>
                                <select name="search-sort" id="uType">
                                    <option selected value="-1">----请选择----</option>
                                    <option value="0">会员</option>
                                    <option value="1">商家</option>
                                    <option value="2">系统管理员</option>
                                </select>
                            </td>
                            <th width="90">姓名:</th>
                            <td><input class="common-text" placeholder="请输入姓名关键词..." id="name"
                                       type="text"></td>
                            <td><input class="btn btn-primary btn2" name="sub" id="sub" value="查询" type="submit"></td>
                        </tr>
                    </table>
                </div>
            </div>
            <!--正文 主体-->
            <div class="result-wrap">
                <form name="myform" id="myform" method="post">
                    <div class="result-title">
                        <div class="result-list">
                            <a href="../static/addUser.html"><i class="icon-font"></i>新增用户</a>
                        </div>
                    </div>
                    <div class="result-content">
                        <table class="table table-hover" id="users">
                        </table>
                    </div>
                </form>
            </div>
        </div>
        <!--footer 页脚-->
        <div class="footer">
            <nav aria-label="Page navigation">
                <ul class="pagination" id="pagination">
                </ul>
            </nav>
            <p id="tips"></p>
        </div>
    </div>
</div>
</body>
</html>